Namespace Workflow

    Partial Class EditCategories
        Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents txtEmail As System.Web.UI.WebControls.TextBox


        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()


            If CategoryId > 0 Then
                Title = "Category #" & CategoryId & ": " & CCategory.Cache.GetById(CategoryId).CategoryName
            End If
        End Sub

#End Region


        Public ReadOnly Property CategoryId() As Integer
            Get
                Return Request.QueryString("categoryId")
            End Get
        End Property

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Page.IsPostBack Then Exit Sub

            dgCategories.DataSource = CCategory.Cache
            dgCategories.DataBind()

            If CategoryId > 0 Then
                With CCategory.Cache.GetById(CategoryId)
                    txtName.Text = .CategoryName
                End With
                litAddEdit.Text = "Edit"
                btnSave.Text = "Update"

                If CategoryId = CConfig.CategoryOther Then btnDelete.Enabled = False
            Else
                btnDelete.Enabled = False
                lnkNew.Visible = False
            End If

            CUtilities.OnReturnPress(txtName, btnSave)
        End Sub

        Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            Dim items As CItemList = CItem.Cache.GetByCategoryId(CategoryId)
            Dim i As CItem
            For Each i In items
                i.ItemCategoryId = CConfig.CategoryOther
                i.Save()
            Next

            With CCategory.Cache.GetById(CategoryId)
                .Delete()
            End With
            Response.Redirect("EditCategories.aspx", True)
        End Sub

        Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim category As New CCategory
            If CategoryId > 0 Then category = CCategory.Cache.GetById(CategoryId)

            With category
                .CategoryName = txtName.Text
                If Len(.CategoryName) = 0 Then .CategoryName = "New Project"
                .Save()

                Response.Redirect("EditCategories.aspx?categoryId=" & .CategoryId, True)
            End With
        End Sub
    End Class
End Namespace
